import logging.handlers

class GetLogger:
    logger = None
    @classmethod
    def get_logger(cls):
        if cls.logger == None:
            #获取日志器
            cls.logger = logging.getLogger()

            cls.logger.setLevel(logging.INFO)

            #获取处理器控制台
            sh = logging.StreamHandler()

            #获取 处理器文件 按时间分隔
            th = logging.handlers.TimedRotatingFileHandler(filename="../log/log1.log",
                                                           when="S",
                                                           interval=1,
                                                           backupCount=30,
                                                           encoding="UTF-8")
            #设置格式器
            fmt = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s (%(funcName)s:%(lineno)d] - %(message)s"
            fm = logging.Formatter(fmt)

            #添加格式器
            sh.setFormatter(fm)
            th.setFormatter(fm)

            #将格式器添加到日志器
            cls.logger.addHandler(sh)
            cls.logger.addHandler(th)
        return cls.logger

if __name__ == "__main__":
    logger = GetLogger().get_logger()

